package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    public void save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //1.如果商品已经存在购物车，只需把该条商品数量加一
        if(shoppingCartList != null &&shoppingCartList.size() > 0){
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart1);
        }
        //2.如果商品不存在于购物车，就插入该条数据
        else{
            Long dish_id = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            //判断是套餐类型还是菜品类型
            //如果是菜品
            if(dish_id != null){
                Dish dish = dishMapper.getById(dish_id);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
            }
            //如果是套餐
            else{
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(setmealId);
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCartMapper.save(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @param id
     * @return
     */
    public List<ShoppingCart> getListByShoppingCart(ShoppingCart shoppingCart) {
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     * @return
     */
    public void deleteShoppingCartList(Long userId) {
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void reduce(ShoppingCartDTO shoppingCartDTO){
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        shoppingCart = shoppingCartList.get(0);
        int number = shoppingCart.getNumber();
        if(shoppingCartList != null && number > 1){
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
        else{
            shoppingCartMapper.deleteByDishIdOrSetmealId(shoppingCart);
        }
    }
}
